﻿using System;
using System.Linq.Expressions;
using FluentExport.CSVExport;
using FluentExport.CSVExport.Interfaces;
using NSubstitute;
using NUnit.Framework;
using Should.Fluent;
using System.Linq;
using System.Collections.Generic;

namespace FluentExport.Tests.CSVExportTests
{
    [TestFixture]
    public class CSVCompoundSourceTests
    {
        [Test]
        public void MustInvokeDefineSourceMethodCorrectly()
        {
            var scsvExportHelper = Substitute.For<ICSVExportHelper>();
            scsvExportHelper.GetPropertyName(Arg.Any<Expression<Func<DummyClassTest, object>>>()).Returns("ID");

            var scsvExport = Substitute.For<ICSVExport>();

            var csvCompoundSource = new CSVCompoundSource<DummyClassTest>(scsvExport, scsvExportHelper);

            csvCompoundSource.Add(d => d.Id);

            csvCompoundSource.Sources.Should().Not.Be.Null();
            csvCompoundSource.Sources.Should().Not.Be.Empty();
            csvCompoundSource.Sources.Count().Should().Equal(1);
            csvCompoundSource.Sources.ElementAt(0).PropertyName.Should().Equal("ID");
        }

        [Test]
        public void MustInvokeWithFormatCorrectly()
        {
            var scsvExportHelper = Substitute.For<ICSVExportHelper>();
            scsvExportHelper.GetPropertyName(Arg.Any<Expression<Func<DummyClassTest, object>>>()).Returns("ID");

            var scsvExport = Substitute.For<ICSVExport>();

            var csvCompoundSource = new CSVCompoundSource<DummyClassTest>(scsvExport, scsvExportHelper);

            var ret = csvCompoundSource.WithFormat("{0},{1}");

            csvCompoundSource.Format.Should().Equal("{0},{1}");
            ret.Should().Not.Be.Null();
        }

        [Test]
        public void MustInvokeGetValueMethodCorrectly()
        {
            var scsvExportHelper = Substitute.For<ICSVExportHelper>();
            var scsvExport = Substitute.For<ICSVExport>();

            var simpleSource1 = Substitute.For<ICSVSimpleSource>();
            var simpleSource2 = Substitute.For<ICSVSimpleSource>();

            simpleSource1.GetValue(Arg.Any<object>()).Returns("22");
            simpleSource2.GetValue(Arg.Any<object>()).Returns("Player1");
            
            var sources = new List<ICSVSimpleSource>
                              {
                                simpleSource1,
                                simpleSource2
                              };

            var csvCompoundSource = new CSVCompoundSource<DummyClassTest>(scsvExport, scsvExportHelper) { Sources = sources };

            csvCompoundSource.WithFormat("{0},{1}");

            var dummyObject = new DummyClassTest();

            var ret = csvCompoundSource.GetValue(dummyObject);

            ret.Should().Not.Be.NullOrEmpty();
            ret.Should().Equal("22,Player1");
        }
    }
}
